<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>selectionmodel.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_ui_selectionmodel.js.html">selectionmodel.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2007 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Single-selection model implemenation.
<a name="line17"></a> *
<a name="line18"></a> * TODO(attila): Add keyboard &amp; mouse event hooks?
<a name="line19"></a> * TODO(attila): Add multiple selection?
<a name="line20"></a> *
<a name="line21"></a> * @author attila@google.com (Attila Bodis)
<a name="line22"></a> */
<a name="line23"></a>
<a name="line24"></a>
<a name="line25"></a>goog.provide(&#39;goog.ui.SelectionModel&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.require(&#39;goog.array&#39;);
<a name="line28"></a>goog.require(&#39;goog.events.EventTarget&#39;);
<a name="line29"></a>goog.require(&#39;goog.events.EventType&#39;);
<a name="line30"></a>
<a name="line31"></a>
<a name="line32"></a>
<a name="line33"></a>/**
<a name="line34"></a> * Single-selection model.  Dispatches a {@link goog.events.EventType.SELECT}
<a name="line35"></a> * event when a selection is made.
<a name="line36"></a> * @param {Array.&lt;Object&gt;=} opt_items Array of items; defaults to empty.
<a name="line37"></a> * @extends {goog.events.EventTarget}
<a name="line38"></a> * @constructor
<a name="line39"></a> */
<a name="line40"></a>goog.ui.SelectionModel = function(opt_items) {
<a name="line41"></a>  goog.events.EventTarget.call(this);
<a name="line42"></a>
<a name="line43"></a>  /**
<a name="line44"></a>   * Array of items controlled by the selection model.  If the items support
<a name="line45"></a>   * the {@code setSelected(Boolean)} interface, they will be (de)selected
<a name="line46"></a>   * as needed.
<a name="line47"></a>   * @type {!Array.&lt;Object&gt;}
<a name="line48"></a>   * @private
<a name="line49"></a>   */
<a name="line50"></a>  this.items_ = [];
<a name="line51"></a>  this.addItems(opt_items);
<a name="line52"></a>};
<a name="line53"></a>goog.inherits(goog.ui.SelectionModel, goog.events.EventTarget);
<a name="line54"></a>
<a name="line55"></a>
<a name="line56"></a>/**
<a name="line57"></a> * The currently selected item (null if none).
<a name="line58"></a> * @type {Object}
<a name="line59"></a> * @private
<a name="line60"></a> */
<a name="line61"></a>goog.ui.SelectionModel.prototype.selectedItem_ = null;
<a name="line62"></a>
<a name="line63"></a>
<a name="line64"></a>/**
<a name="line65"></a> * Selection handler function.  Called with two arguments (the item to be
<a name="line66"></a> * selected or deselected, and a Boolean indicating whether the item is to
<a name="line67"></a> * be selected or deselected).
<a name="line68"></a> * @type {Function}
<a name="line69"></a> * @private
<a name="line70"></a> */
<a name="line71"></a>goog.ui.SelectionModel.prototype.selectionHandler_ = null;
<a name="line72"></a>
<a name="line73"></a>
<a name="line74"></a>/**
<a name="line75"></a> * Returns the selection handler function used by the selection model to change
<a name="line76"></a> * the internal selection state of items under its control.
<a name="line77"></a> * @return {Function} Selection handler function (null if none).
<a name="line78"></a> */
<a name="line79"></a>goog.ui.SelectionModel.prototype.getSelectionHandler = function() {
<a name="line80"></a>  return this.selectionHandler_;
<a name="line81"></a>};
<a name="line82"></a>
<a name="line83"></a>
<a name="line84"></a>/**
<a name="line85"></a> * Sets the selection handler function to be used by the selection model to
<a name="line86"></a> * change the internal selection state of items under its control.  The
<a name="line87"></a> * function must take two arguments:  an item and a Boolean to indicate whether
<a name="line88"></a> * the item is to be selected or deselected.  Selection handler functions are
<a name="line89"></a> * only needed if the items in the selection model don&#39;t natively support the
<a name="line90"></a> * {@code setSelected(Boolean)} interface.
<a name="line91"></a> * @param {Function} handler Selection handler function.
<a name="line92"></a> */
<a name="line93"></a>goog.ui.SelectionModel.prototype.setSelectionHandler = function(handler) {
<a name="line94"></a>  this.selectionHandler_ = handler;
<a name="line95"></a>};
<a name="line96"></a>
<a name="line97"></a>
<a name="line98"></a>/**
<a name="line99"></a> * Returns the number of items controlled by the selection model.
<a name="line100"></a> * @return {number} Number of items.
<a name="line101"></a> */
<a name="line102"></a>goog.ui.SelectionModel.prototype.getItemCount = function() {
<a name="line103"></a>  return this.items_.length;
<a name="line104"></a>};
<a name="line105"></a>
<a name="line106"></a>
<a name="line107"></a>/**
<a name="line108"></a> * Returns the 0-based index of the given item within the selection model, or
<a name="line109"></a> * -1 if no such item is found.
<a name="line110"></a> * @param {Object|undefined} item Item to look for.
<a name="line111"></a> * @return {number} Index of the given item (-1 if none).
<a name="line112"></a> */
<a name="line113"></a>goog.ui.SelectionModel.prototype.indexOfItem = function(item) {
<a name="line114"></a>  return item ? goog.array.indexOf(this.items_, item) : -1;
<a name="line115"></a>};
<a name="line116"></a>
<a name="line117"></a>
<a name="line118"></a>/**
<a name="line119"></a> * @return {Object|undefined} The first item, or undefined if there are no items
<a name="line120"></a> *     in the model.
<a name="line121"></a> */
<a name="line122"></a>goog.ui.SelectionModel.prototype.getFirst = function() {
<a name="line123"></a>  return this.items_[0];
<a name="line124"></a>};
<a name="line125"></a>
<a name="line126"></a>
<a name="line127"></a>/**
<a name="line128"></a> * @return {Object|undefined} The last item, or undefined if there are no items
<a name="line129"></a> *     in the model.
<a name="line130"></a> */
<a name="line131"></a>goog.ui.SelectionModel.prototype.getLast = function() {
<a name="line132"></a>  return this.items_[this.items_.length - 1];
<a name="line133"></a>};
<a name="line134"></a>
<a name="line135"></a>
<a name="line136"></a>/**
<a name="line137"></a> * Returns the item at the given 0-based index.
<a name="line138"></a> * @param {number} index Index of the item to return.
<a name="line139"></a> * @return {Object} Item at the given index (null if none).
<a name="line140"></a> */
<a name="line141"></a>goog.ui.SelectionModel.prototype.getItemAt = function(index) {
<a name="line142"></a>  return this.items_[index] || null;
<a name="line143"></a>};
<a name="line144"></a>
<a name="line145"></a>
<a name="line146"></a>/**
<a name="line147"></a> * Bulk-adds items to the selection model.  This is more efficient than calling
<a name="line148"></a> * {@link #addItem} for each new item.
<a name="line149"></a> * @param {Array.&lt;Object&gt;|undefined} items New items to add.
<a name="line150"></a> */
<a name="line151"></a>goog.ui.SelectionModel.prototype.addItems = function(items) {
<a name="line152"></a>  if (items) {
<a name="line153"></a>    // New items shouldn&#39;t be selected.
<a name="line154"></a>    goog.array.forEach(items, function(item) {
<a name="line155"></a>      this.selectItem_(item, false);
<a name="line156"></a>    }, this);
<a name="line157"></a>    goog.array.extend(this.items_, items);
<a name="line158"></a>  }
<a name="line159"></a>};
<a name="line160"></a>
<a name="line161"></a>
<a name="line162"></a>/**
<a name="line163"></a> * Adds an item at the end of the list.
<a name="line164"></a> * @param {Object} item Item to add.
<a name="line165"></a> */
<a name="line166"></a>goog.ui.SelectionModel.prototype.addItem = function(item) {
<a name="line167"></a>  this.addItemAt(item, this.getItemCount());
<a name="line168"></a>};
<a name="line169"></a>
<a name="line170"></a>
<a name="line171"></a>/**
<a name="line172"></a> * Adds an item at the given index.
<a name="line173"></a> * @param {Object} item Item to add.
<a name="line174"></a> * @param {number} index Index at which to add the new item.
<a name="line175"></a> */
<a name="line176"></a>goog.ui.SelectionModel.prototype.addItemAt = function(item, index) {
<a name="line177"></a>  if (item) {
<a name="line178"></a>    // New items must not be selected.
<a name="line179"></a>    this.selectItem_(item, false);
<a name="line180"></a>    goog.array.insertAt(this.items_, item, index);
<a name="line181"></a>  }
<a name="line182"></a>};
<a name="line183"></a>
<a name="line184"></a>
<a name="line185"></a>/**
<a name="line186"></a> * Removes the given item (if it exists).  Dispatches a {@code SELECT} event if
<a name="line187"></a> * the removed item was the currently selected item.
<a name="line188"></a> * @param {Object} item Item to remove.
<a name="line189"></a> */
<a name="line190"></a>goog.ui.SelectionModel.prototype.removeItem = function(item) {
<a name="line191"></a>  if (item &amp;&amp; goog.array.remove(this.items_, item)) {
<a name="line192"></a>    if (item == this.selectedItem_) {
<a name="line193"></a>      this.selectedItem_ = null;
<a name="line194"></a>      this.dispatchEvent(goog.events.EventType.SELECT);
<a name="line195"></a>    }
<a name="line196"></a>  }
<a name="line197"></a>};
<a name="line198"></a>
<a name="line199"></a>
<a name="line200"></a>/**
<a name="line201"></a> * Removes the item at the given index.
<a name="line202"></a> * @param {number} index Index of the item to remove.
<a name="line203"></a> */
<a name="line204"></a>goog.ui.SelectionModel.prototype.removeItemAt = function(index) {
<a name="line205"></a>  this.removeItem(this.getItemAt(index));
<a name="line206"></a>};
<a name="line207"></a>
<a name="line208"></a>
<a name="line209"></a>/**
<a name="line210"></a> * @return {Object} The currently selected item, or null if none.
<a name="line211"></a> */
<a name="line212"></a>goog.ui.SelectionModel.prototype.getSelectedItem = function() {
<a name="line213"></a>  return this.selectedItem_;
<a name="line214"></a>};
<a name="line215"></a>
<a name="line216"></a>
<a name="line217"></a>/**
<a name="line218"></a> * @return {!Array.&lt;Object&gt;} All items in the selection model.
<a name="line219"></a> */
<a name="line220"></a>goog.ui.SelectionModel.prototype.getItems = function() {
<a name="line221"></a>  return goog.array.clone(this.items_);
<a name="line222"></a>};
<a name="line223"></a>
<a name="line224"></a>
<a name="line225"></a>/**
<a name="line226"></a> * Selects the given item, deselecting any previously selected item, and
<a name="line227"></a> * dispatches a {@code SELECT} event.
<a name="line228"></a> * @param {Object} item Item to select (null to clear the selection).
<a name="line229"></a> */
<a name="line230"></a>goog.ui.SelectionModel.prototype.setSelectedItem = function(item) {
<a name="line231"></a>  if (item != this.selectedItem_) {
<a name="line232"></a>    this.selectItem_(this.selectedItem_, false);
<a name="line233"></a>    this.selectedItem_ = item;
<a name="line234"></a>    this.selectItem_(item, true);
<a name="line235"></a>  }
<a name="line236"></a>
<a name="line237"></a>  // Always dispatch a SELECT event; let listeners decide what to do if the
<a name="line238"></a>  // selected item hasn&#39;t changed.
<a name="line239"></a>  this.dispatchEvent(goog.events.EventType.SELECT);
<a name="line240"></a>};
<a name="line241"></a>
<a name="line242"></a>
<a name="line243"></a>/**
<a name="line244"></a> * @return {number} The 0-based index of the currently selected item, or -1
<a name="line245"></a> *     if none.
<a name="line246"></a> */
<a name="line247"></a>goog.ui.SelectionModel.prototype.getSelectedIndex = function() {
<a name="line248"></a>  return this.indexOfItem(this.selectedItem_);
<a name="line249"></a>};
<a name="line250"></a>
<a name="line251"></a>
<a name="line252"></a>/**
<a name="line253"></a> * Selects the item at the given index, deselecting any previously selected
<a name="line254"></a> * item, and dispatches a {@code SELECT} event.
<a name="line255"></a> * @param {number} index Index to select (-1 to clear the selection).
<a name="line256"></a> */
<a name="line257"></a>goog.ui.SelectionModel.prototype.setSelectedIndex = function(index) {
<a name="line258"></a>  this.setSelectedItem(this.getItemAt(index));
<a name="line259"></a>};
<a name="line260"></a>
<a name="line261"></a>
<a name="line262"></a>/**
<a name="line263"></a> * Clears the selection model by removing all items from the selection.
<a name="line264"></a> */
<a name="line265"></a>goog.ui.SelectionModel.prototype.clear = function() {
<a name="line266"></a>  goog.array.clear(this.items_);
<a name="line267"></a>  this.selectedItem_ = null;
<a name="line268"></a>};
<a name="line269"></a>
<a name="line270"></a>
<a name="line271"></a>/** @override */
<a name="line272"></a>goog.ui.SelectionModel.prototype.disposeInternal = function() {
<a name="line273"></a>  goog.ui.SelectionModel.superClass_.disposeInternal.call(this);
<a name="line274"></a>  delete this.items_;
<a name="line275"></a>  this.selectedItem_ = null;
<a name="line276"></a>};
<a name="line277"></a>
<a name="line278"></a>
<a name="line279"></a>/**
<a name="line280"></a> * Private helper; selects or deselects the given item based on the value of
<a name="line281"></a> * the {@code select} argument.  If a selection handler has been registered
<a name="line282"></a> * (via {@link #setSelectionHandler}, calls it to update the internal selection
<a name="line283"></a> * state of the item.  Otherwise, attempts to call {@code setSelected(Boolean)}
<a name="line284"></a> * on the item itself, provided the object supports that interface.
<a name="line285"></a> * @param {Object} item Item to select or deselect.
<a name="line286"></a> * @param {boolean} select If true, the object will be selected; if false, it
<a name="line287"></a> *     will be deselected.
<a name="line288"></a> * @private
<a name="line289"></a> */
<a name="line290"></a>goog.ui.SelectionModel.prototype.selectItem_ = function(item, select) {
<a name="line291"></a>  if (item) {
<a name="line292"></a>    if (typeof this.selectionHandler_ == &#39;function&#39;) {
<a name="line293"></a>      // Use the registered selection handler function.
<a name="line294"></a>      this.selectionHandler_(item, select);
<a name="line295"></a>    } else if (typeof item.setSelected == &#39;function&#39;) {
<a name="line296"></a>      // Call setSelected() on the item, if it supports it.
<a name="line297"></a>      item.setSelected(select);
<a name="line298"></a>    }
<a name="line299"></a>  }
<a name="line300"></a>};
</pre>


</body>
</html>
